﻿#include "MFD.h"

PIMAGE mfdImg = nullptr;  // 声明为全局变量

void drawMFD(int x0, int y0, double height)
{
    ege_enable_aa(true, mfdImg);
    double mfdWidth = height * 0.728;
    double mfdHeight = height;
    double mfdScale = height / 1000.0f;

    // 检查是否需要创建新图像
    if (mfdImg == nullptr ||
        getwidth(mfdImg) != mfdWidth ||
        getheight(mfdImg) != mfdHeight) {
        // 释放旧图像
        if (mfdImg != nullptr) {
            delimage(mfdImg);
        }
        // 创建新图像
        mfdImg = newimage(mfdWidth, mfdHeight);
        if (mfdImg == nullptr) {
            // 处理创建失败的情况
            return;
        }
        setbkcolor(EGEARGB(0, 0, 0, 0), mfdImg);
    }

    // 清空图像（可选，根据需求决定是否保留）
    //cleardevice(mfdImg);

    // 设置窗口标题（通常只需在初始化时设置一次）
    static bool captionSet = false;
    if (!captionSet) {
        setcaption("mfd");
        captionSet = true;
    }
    //setbkcolor(EGEARGB(255, 38, 50, 64));
    // FMS 1
    drawFMS1(mfdScale);

    // 绘制图像
    //putimage(x0, y0, mfdImg);
}

void drawFMS1(double mfdScale)
{
    // 上面的一些东西
    setfont(30 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setcolor(EGEARGB(255, 120, 120, 120), mfdImg);
    settextjustify(LEFT_TEXT, TOP_TEXT, mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    ege_outtextxy(23 * mfdScale, 4 * mfdScale, "PRESS FOR KEY INPUT", mfdImg);
    

    setcolor(EGEARGB(255, 127, 127, 127), mfdImg);
    ege_rectangle(21 * mfdScale, 61 * mfdScale, 178 * mfdScale, 31 * mfdScale, mfdImg);
    setcolor(EGEARGB(255, 28, 236, 231), mfdImg);
    
    ege_outtextxy(29 * mfdScale, 61 * mfdScale, "FMS 1", mfdImg);
    // 三角形
    drawTriangle(168 * mfdScale, 67 * mfdScale, 17.32f * mfdScale, mfdScale);

    setfillcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    ege_fillrect(20 * mfdScale, 140 * mfdScale, 689 * mfdScale, 21 * mfdScale, mfdImg);

    setfont(27 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setcolor(EGEARGB(255, 127, 127, 127), mfdImg);
    ege_outtextxy(29 * mfdScale, 137 * mfdScale, "ACTIVE / F-PLN", mfdImg);
    ege_outtextxy(659 * mfdScale, 137 * mfdScale, "1 20", mfdImg);
    // 长条填充
    setfillcolor(EGEARGB(255, 64, 64, 64), mfdImg);
    ege_fillrect(21 * mfdScale, 92 * mfdScale, 688 * mfdScale, 47 * mfdScale, mfdImg);
    // 第一个
    setcolor(EGEARGB(255, 127, 127, 127), mfdImg);
    ege_rectangle(21 * mfdScale, 92 * mfdScale, 178 * mfdScale, 47 * mfdScale, mfdImg);
    ege_rectangle(198 * mfdScale, 92 * mfdScale, 170 * mfdScale, 47 * mfdScale, mfdImg);
    ege_rectangle(368 * mfdScale, 92 * mfdScale, 170 * mfdScale, 47 * mfdScale, mfdImg);
    ege_rectangle(538 * mfdScale, 92 * mfdScale, 170 * mfdScale, 47 * mfdScale, mfdImg);

    setcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    //setfont(40 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    ege_outtextxy(32 * mfdScale, 101 * mfdScale, "ACTIVE", mfdImg);
    ege_outtextxy(205 * mfdScale, 101 * mfdScale, "POSITION", mfdImg);
    ege_outtextxy(377 * mfdScale, 101 * mfdScale, "SEC INDEX", mfdImg);
    ege_outtextxy(548 * mfdScale, 101 * mfdScale, "DATA", mfdImg);

    drawTriangle(169 * mfdScale, 108 * mfdScale, 17.32f * mfdScale, mfdScale);
    drawTriangle(340 * mfdScale, 108 * mfdScale, 17.32f * mfdScale, mfdScale);
    drawTriangle(512 * mfdScale, 108 * mfdScale, 17.32f * mfdScale, mfdScale);
    drawTriangle(678 * mfdScale, 108 * mfdScale, 17.32f * mfdScale, mfdScale);
    
    //内容
    //setfont(30 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    ege_outtextxy(37 * mfdScale, 175 * mfdScale, "FROM", mfdImg);

    ege_outtextxy(185 * mfdScale, 175 * mfdScale, "TIME", mfdImg);
    drawMyRect(287 * mfdScale, 175 * mfdScale, 208 * mfdScale, 30 * mfdScale, mfdScale);
    setfont(30 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    setcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    ege_outtextxy(298 * mfdScale, 175 * mfdScale, "SPD", mfdImg);
    ege_outtextxy(386 * mfdScale, 175 * mfdScale, "ALT", mfdImg);
    drawTriangle(465 * mfdScale, 182 * mfdScale, 17.32f * mfdScale, mfdScale);
    ege_outtextxy(523 * mfdScale, 175 * mfdScale, "TRK", mfdImg);
    ege_outtextxy(590 * mfdScale, 175 * mfdScale, "DIST", mfdImg);
    ege_outtextxy(660 * mfdScale, 175 * mfdScale, "FPA", mfdImg);

    // REAL CONTENT
    drawDashed(25 * mfdScale, 249 * mfdScale, mfdScale, 9);
    drawDashed(481 * mfdScale, 249 * mfdScale, mfdScale, 8);
    drawDashed(25 * mfdScale, 308 * mfdScale, mfdScale, 9);
    drawDashed(481 * mfdScale, 308 * mfdScale, mfdScale, 8);
    ege_outtextxy(287 * mfdScale, 232 * mfdScale, "END OF F-PIN", mfdImg);
    ege_outtextxy(287 * mfdScale, 292 * mfdScale, "NO ALTN F-PIN", mfdImg);

    setcolor(EGEARGB(255, 127, 127, 127), mfdImg);
    ege_line(21 * mfdScale, 211 * mfdScale, 710 * mfdScale, 211 * mfdScale, mfdImg);
    ege_line(21 * mfdScale, 784 * mfdScale, 710 * mfdScale, 784 * mfdScale, mfdImg);
    ege_line(21 * mfdScale, 858 * mfdScale, 710 * mfdScale, 858 * mfdScale, mfdImg);
    ege_line(21 * mfdScale, 918 * mfdScale, 710 * mfdScale, 918 * mfdScale, mfdImg);

    drawMyRect(535 * mfdScale, 804 * mfdScale, 31 * mfdScale, 41 * mfdScale, mfdScale);
    drawMyRect(575 * mfdScale, 804 * mfdScale, 31 * mfdScale, 41 * mfdScale, mfdScale);
    drawMyRect(615 * mfdScale, 804 * mfdScale, 100 * mfdScale, 41 * mfdScale, mfdScale);

    drawMyRect(21 * mfdScale, 871 * mfdScale, 110 * mfdScale, 32 * mfdScale, mfdScale);
    drawMyRect(272 * mfdScale, 871 * mfdScale, 208 * mfdScale, 32 * mfdScale, mfdScale);
    drawMyRect(615 * mfdScale, 871 * mfdScale, 100 * mfdScale, 32 * mfdScale, mfdScale);

    drawMyRect(8 * mfdScale, 927 * mfdScale, 97 * mfdScale, 65 * mfdScale, mfdScale);

    drawTriangle(581 * mfdScale, 811 * mfdScale, 17.32f * mfdScale, mfdScale);
    drawTriangle(581 * mfdScale, 820 * mfdScale, 17.32f * mfdScale, mfdScale);

    drawTriangle(541 * mfdScale, 829 * mfdScale, -17.32f * mfdScale, mfdScale);
    drawTriangle(541 * mfdScale, 838 * mfdScale, -17.32f * mfdScale, mfdScale);

    drawTriangle(452 * mfdScale, 895 * mfdScale, -17.32f * mfdScale, mfdScale);


    setcolor(EGEARGB(255, 31, 194, 32), mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    ege_outtextxy(640 * mfdScale, 964 * mfdScale, "CAPT", mfdImg);

    setfont(30 * mfdScale, 0, "MS Reference Sans Serif", mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    setcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    ege_outtextxy(632 * mfdScale, 811 * mfdScale, "DEST", mfdImg);
    ege_outtextxy(622 * mfdScale, 872 * mfdScale, "DIR TO", mfdImg);
    ege_outtextxy(282 * mfdScale, 872 * mfdScale, "F-PLN", mfdImg);
    ege_outtextxy(372 * mfdScale, 872 * mfdScale, "INFO", mfdImg);
    ege_outtextxy(48 * mfdScale, 872 * mfdScale, "INIT", mfdImg);
    ege_outtextxy(16 * mfdScale, 931 * mfdScale, "CLEAR", mfdImg);
    ege_outtextxy(26 * mfdScale, 958 * mfdScale, "INFO", mfdImg);


}

void drawDashed(double x0, double y0, double mfdScale, int num)
{
    for (int i = 0; i < num; i++)
    {
        setcolor(EGEARGB(255, 255, 255, 255), mfdImg);
        ege_line(x0 + i * 30.0 * mfdScale, y0, x0 + ( 7 + i * 30.0) * mfdScale, y0, mfdImg);
    }
}

void drawTriangle(double x0, double y0, double height, double mfdScale)
{
    // 右边的三角形
    ege_point triangle[] = {
        x0, y0,
        (x0 + 20.0f * mfdScale), y0,
        (x0 + 10.0f * mfdScale), (y0 + height),
    };

    setfillcolor(EGEARGB(255, 255, 255, 255), mfdImg);
    ege_fillpoly(sizeof(triangle) / sizeof(triangle[0]), triangle, mfdImg);
}

void drawMyRect(double x0, double y0, double width, double height, double mfdScale)
{
    setfillcolor(EGEARGB(255, 64, 64, 64), mfdImg);
    ege_fillrect(x0, y0, width, height, mfdImg);
    setcolor(EGEARGB(255, 127, 127, 127), mfdImg);
    setlinewidth(2 * mfdScale, mfdImg);
    ege_rectangle(x0, y0, width, height, mfdImg);
}

// 在程序结束时释放资源
void cleanupMFD() {
    if (mfdImg != nullptr) {
        delimage(mfdImg);
        mfdImg = nullptr;
    }
}